// 高德地图定位工具类
const amapKey = '908041d2ef4d3a8f5f9574dad6638135'; // 请替换为您的高德地图AppKey

// 获取当前位置
export const getCurrentLocation = () => {
    return new Promise((resolve, reject) => {
        wx.getLocation({
            type: 'gcj02',
            success: (res) => {
                resolve({
                    latitude: res.latitude,
                    longitude: res.longitude
                });
            },
            fail: (error) => {
                console.error('获取经纬度失败:', error);
                reject(error);
            }
        });
    });
};

// 检查定位权限
export const checkLocationPermission = () => {
    return new Promise((resolve, reject) => {
        wx.getLocation({
            type: 'gcj02',
            success: () => {
                resolve(true);
            },
            fail: (error) => {
                console.error('定位权限检查失败:', error);
                if (error.errMsg.indexOf('auth deny') >= 0) {
                    wx.showModal({
                        title: '提示',
                        content: '需要获取您的地理位置，请确认授权',
                        success: (res) => {
                            if (res.confirm) {
                                wx.openSetting({
                                    success: (settingRes) => {
                                        if (settingRes.authSetting['scope.userLocation']) {
                                            resolve(true);
                                        } else {
                                            reject(new Error('用户拒绝授权'));
                                        }
                                    }
                                });
                            } else {
                                reject(new Error('用户拒绝授权'));
                            }
                        }
                    });
                } else {
                    reject(error);
                }
            }
        });
    });
}; 